package jp.aist.gtrc.plus.scheduler.algorithm;

import java.util.*;

import provisioner.Resource;

public class ResourceSetMap {
   private Map<String,Set<Resource>> map;
   private Map<Resource,String> typeMap;
   
   public ResourceSetMap() {
      this.map = new HashMap<String,Set<Resource>>();
      this.typeMap = new HashMap<Resource,String>();
   }

   public synchronized void addResource(String type, Resource resource) {
      Set<Resource> resources = map.get(type);
      if (resources == null) {
         resources = new HashSet<Resource>();
         map.put(type,resources);
      }
      resources.add(resource);
      typeMap.put(resource,type);
   }

   public synchronized void removeResource(Resource resource) {
      String resourceType = typeMap.get(resource);
      Set<Resource> resources = map.get(resourceType);
      if (resources  != null) {
          resources.remove(resource);
      }
      typeMap.remove(resource);
   }

   public synchronized Set<Resource> getResources() {
      return typeMap.keySet();
   }

   public synchronized Set<Resource> getResourcesByType(String type) {
      return map.get(type);
   }

   public synchronized Set<String> getTypes() {
      return map.keySet();
   }

   public synchronized String getType(Resource resource) {
      return typeMap.get(resource);
   }

}
